/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.modules.auction.service.impl;

import cn.hutool.core.util.ObjectUtil;
import co.yixiang.api.YshopException;
import co.yixiang.domain.BaseDomain;
import co.yixiang.enums.BillDetailEnum;
import co.yixiang.enums.BillEnum;
import co.yixiang.modules.auction.domain.AuctionOrder;
import co.yixiang.modules.auction.domain.AuctionRecord;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.modules.auction.domain.YxAuctionConfig;
import co.yixiang.modules.auction.domain.YxAuctionOrder;
import co.yixiang.modules.auction.service.YxAuctionOrderService;
import co.yixiang.modules.auction.service.dto.SingleDto;
import co.yixiang.modules.auction.service.mapper.AuctionOrderMapper;
import co.yixiang.modules.auction.service.mapper.YxAuctionOrderMapper;
import co.yixiang.modules.user.domain.YxUser;
import co.yixiang.modules.user.domain.YxUserBill;
import co.yixiang.modules.user.service.YxUserBillService;
import co.yixiang.modules.user.service.YxUserService;
import co.yixiang.utils.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.AllArgsConstructor;
import co.yixiang.dozer.service.IGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.modules.auction.service.AuctionRecordService;
import co.yixiang.modules.auction.service.dto.AuctionRecordDto;
import co.yixiang.modules.auction.service.dto.AuctionRecordQueryCriteria;
import co.yixiang.modules.auction.service.mapper.AuctionRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ycl
* @date 2021-02-18
*/
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "AuctionRecord")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AuctionRecordServiceImpl extends BaseServiceImpl<AuctionRecordMapper, AuctionRecord> implements AuctionRecordService {

    private final IGenerator generator;
    private final YxAuctionOrderMapper yxAuctionOrderMapper;
    private final YxUserService userService;
    private final YxAuctionOrderService auctionOrderService;

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(AuctionRecordQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<AuctionRecordDto> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), AuctionRecordDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<AuctionRecordDto> queryAll(AuctionRecordQueryCriteria criteria){

        return baseMapper.selectAuctionRecordList(criteria);

//        return baseMapper.selectList(QueryHelpPlus.getPredicate(AuctionRecord.class, criteria));
    }


    @Override
    public void download(List<AuctionRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AuctionRecordDto AuctionRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("拍卖商品id", AuctionRecord.getAuctionProductId());
            map.put("拍卖价", AuctionRecord.getPrice());
            map.put("封顶价", AuctionRecord.getTopPrice());
            map.put("手续费", AuctionRecord.getServiceCharge());
            map.put("一级分佣0-10", AuctionRecord.getFirstCommission());
            map.put("二级分佣0-10小于一及分佣", AuctionRecord.getSecondCommission());
            map.put("抢拍开始时间", AuctionRecord.getStartTime());
            map.put("时间段", AuctionRecord.getTimeId());
            map.put("状态0等待补拍1补拍中2补拍完成", AuctionRecord.getStatus());
            map.put("转拍用户id", AuctionRecord.getUserId());
            map.put("是否已被购买0否1是", AuctionRecord.getIsBuy());
            map.put("父抢拍单id", AuctionRecord.getParentId());
            map.put("凭证", AuctionRecord.getConfirmVoucher());
            map.put("支付方式1支付宝2微信3余额4银联", AuctionRecord.getPayType());
            map.put(" updateTime",  AuctionRecord.getUpdateTime());
            map.put("创建时间", AuctionRecord.getCreateTime());
            map.put(" isMax",  AuctionRecord.getIsMax());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    /**
     * 获取商品列表
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> getAuctionProduct() {
        return baseMapper.getAuctionProduct();
    }

    /**
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String confirmAuction(Long  id) {
        AuctionRecord yxAuctionRecord=this.baseMapper.selectById(id);
        if(null==yxAuctionRecord){
            throw new YshopException("订单不存在");
        }
        if(2!= yxAuctionRecord.getStatus()){
            throw new YshopException("该卖单状态不为待确定状态");
        }
        //修改卖单表
        yxAuctionRecord.setStatus(3);
        this.baseMapper.updateById(yxAuctionRecord);
        //查询买家订单
        YxAuctionOrder auctionOrder = yxAuctionOrderMapper.selectByAuctionRecord(yxAuctionRecord.getId());
        //修改买家订单
        if(null !=auctionOrder){
            auctionOrder.setStatus(2);
            yxAuctionOrderMapper.updateById(auctionOrder);
        }

        //推送给买家
        YxUser user = userService.getById(auctionOrder.getUserId());
        if (ObjectUtil.isNotEmpty(user)) PushtoSingle.sendToAlias("卖家已确认","卖家已确认，请继续操作",user.getClientId(),1,1L);

        return null;
    }

    /**
     * 获取当天拍卖商品
     *
     * @return
     */
    @Override
    public List<SingleDto> singleList(String productName) {
        return baseMapper.singleList(productName);
    }

    /**
     * 收单
     *
     * @param singleDtoList
     * @param uid
     */
    @Override
    public void confirmSingle(List<SingleDto> singleDtoList, Long uid) {
        auctionOrderService.singleOrder(singleDtoList, uid);
    }

}
